/**
* This file is part of tictionary.
*
* tictionary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* http://code.google.com/p/tictionary/
*/

/*
 * SmallSet.java
 * Created on January 20, 2008, 10:51 AM
 */

package tictionary.dict;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Erol Hira
 */
public class LetterSet {        
    
    private Letter parentLetter;
    private ArrayList<Letter> letters;
    
    public LetterSet(Letter parentLetter) {
        this.parentLetter = parentLetter;
        letters = new ArrayList<Letter>();
    }
    
    //----------------------------------------------------------------------
    
    public int getLetterCount(){
        return letters.size();
    }
    
    public void appendLetter(Letter letter){
        if(letters.contains(letter) == false){
            letters.add(letter);
        }        
    }
    
    public Letter appendLetter(char ch){
        Letter letter = new Letter(ch, this);
        appendLetter(letter);
        return letter;
    }
    
    public Letter giveLetter(char ch){
        
        Iterator<Letter> iter = letters.iterator();
        while(iter.hasNext()){
            Letter letter = iter.next();
            if(letter.getLetter() == ch){
                return letter;
            }
        }
        
        return null;
    }
    
    public Letter forcedGiveLetter(char ch){
        
        Letter letter = giveLetter(ch);
        if(letter == null){
            letter = new Letter(ch, this);
            appendLetter(letter);            
        }
        
        return letter;
    }
    
    public void extractLetterFromSet(int index){
        letters.remove(index);
    }
    
    public Letter getLetter(int i){
        return letters.get(i);
    }

    public Letter getParentLetter() {
        return parentLetter;
    }
    
    public LetterSet getParentSet(){
        return getParentLetter().getSelfSet();
    }

    public void setParentLetter(Letter parentLetter) {
        this.parentLetter = parentLetter;
    }
    
}
